﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LinWon.Core
{
    public class LwException:ApplicationException
    {
        private int _errcode;        
        private static Dictionary<int, string> _cache;
                        
        /// <summary>
        /// 获取错误消息
        /// </summary>
        public string LwMessage
        {
            get {
                if (null == _cache)
                    FillCache();
                if (_cache.ContainsKey(this._errcode))
                    return _cache[this._errcode];
                else
                    return "未找到错误消息" + this._errcode + "，请与管理员联系";
            }
        }

        private static void FillCache()
        {
            _cache = new Dictionary<int, string>();
            string sql = "SELECT Code,ErrMsg FROM ErrorCode";

            Data.SqlHelper helper = LinWon.Core.Data.DataFactory.GetHelper();
            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                dr = helper.ExecuteReader(sql);
                while (dr.Read())
                {
                    int code = dr.GetInt32(0);
                    string msg = dr.GetString(1);
                    _cache.Add(code, msg);
                }
            }
            finally
            {
                if (null != dr && !dr.IsClosed)
                    dr.Close();
            }
        }

        public static void ClearCache()
        {
            _cache = null;
        }

        public LwException(int errcode)
        {
            this._errcode = errcode;
        }

        
    }
}
